/*
 * Powered By fanrenqij.com
 * Since 2017 - 2017
 */

package com.fanrenqiji.modules.business.feedback.service.impl;

import com.fanrenqiji.common.persistence.Page;
import com.fanrenqiji.entity.FeedbackDetailEntity;
import com.fanrenqiji.entity.FeedbackEntity;
import com.fanrenqiji.modules.business.feedback.dao.FeedbackDao;
import com.fanrenqiji.modules.business.feedback.service.FeedbackService;
import com.fanrenqiji.modules.business.feedback.vo.FeedbackVO;
import com.fanrenqiji.modules.business.feedbackdetail.dao.FeedbackDetailDao;
import com.fanrenqiji.modules.business.teacher.dao.TeacherDao;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.dozer.DozerBeanMapper;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

/**
 * 问题反馈业务逻辑实现
 *
 * @author wentao
 */
@Service(value = "feedback")
@Transactional(readOnly = true)
public class FeedbackServiceImpl implements FeedbackService {

    @Autowired
    private FeedbackDao feedbackDao;

    @Autowired
    private FeedbackDetailDao feedbackDetailDao;

    @Autowired
    private TeacherDao teacherDao;

    @Autowired
    DozerBeanMapper beanMapper;

    /**
     * 插入
     *
     * @param feedback 插入的对象
     * @return 插入数据条数
     */
    @Transactional
    public FeedbackVO insert(FeedbackVO feedback) {
        FeedbackEntity feedbackEntity = new FeedbackEntity();
        beanMapper.map(feedback, feedbackEntity);
        feedbackDao.save(feedbackEntity);
        beanMapper.map(feedbackEntity, feedback);
        return feedback;
    }


    /**
     * 批量插入
     *
     * @param list 数据列表
     * @return 插入数据条数
     */
    @Transactional
    public void insertList(List<FeedbackVO> list) {
        for (FeedbackVO feedback : list) {
            insert(feedback);
        }
    }


    /**
     * 修改
     *
     * @param feedback 修改的数据
     * @return 修改的数据
     */
    @Transactional
    public FeedbackVO update(FeedbackVO feedback) {
        FeedbackEntity feedbackEntity = null;
        if (StringUtils.isNotEmpty(feedback.getId())) {
            feedbackEntity = feedbackDao.findOne(Long.parseLong(feedback.getId()));
        }
        if (feedbackEntity == null) {
            feedbackEntity = new FeedbackEntity();
        }
        beanMapper.map(feedback, feedbackEntity);
        /*feedbackEntity.setProcessTime(new Timestamp(new Date().getTime()));
        feedbackDao.save(feedbackEntity);
        if (StringUtils.isNotBlank(feedback.getResult())){
            FeedbackDetailEntity fde=new FeedbackDetailEntity();
            fde.setContent(feedbackEntity.getResult());
            fde.setFeedback(feedbackEntity);
            fde.setReplyTime(new Timestamp(new Date().getTime()));
            feedbackDetailDao.save(fde);
        }*/
        beanMapper.map(feedbackEntity, feedback);
        return feedback;
    }

    @Transactional
    public FeedbackVO allotsave(FeedbackVO feedback) {
        FeedbackEntity feedbackEntity = null;
        if (StringUtils.isNotEmpty(feedback.getId())) {
            feedbackEntity = feedbackDao.findOne(Long.parseLong(feedback.getId()));
        }
        if (feedbackEntity == null) {
            feedbackEntity = new FeedbackEntity();
        }
        /*//设置状态
        if(StringUtils.isNotBlank(feedback.getStatus())){
            feedbackEntity.setStatus(Integer.parseInt(feedback.getStatus()));
        }*/
        //设置恢复老师
        feedbackDao.save(feedbackEntity);
        beanMapper.map(feedbackEntity, feedback);
        return feedback;
    }


    /**
     * 删除
     *
     * @param id 数据标识
     * @return 删除的数据条数
     */
    @Transactional
    public void delete(String id) {
        feedbackDao.deleteById(Long.parseLong(id));
    }


    /**
     * 查询所有数据
     *
     * @return 数据列表
     */
    public List<FeedbackVO> getAll() {
        List<FeedbackVO> list = Lists.newArrayList();
        List<FeedbackEntity> entities = feedbackDao.findAllList();
        for (FeedbackEntity feedbackEntity : entities) {
            FeedbackVO feedbackVO = new FeedbackVO();
            beanMapper.map(feedbackEntity, feedbackVO);
            list.add(feedbackVO);
        }
        return list;
    }


    /**
     * 通过条件查询数据列表
     *
     * @param feedback 查询条件
     * @return 数据列表
     */
    public List<FeedbackVO> getList(FeedbackVO feedback) {
        List<FeedbackVO> list = Lists.newArrayList();
        DetachedCriteria dc = feedbackDao.createDetachedCriteria();
        dc.add(Restrictions.eq(FeedbackEntity.DEL_FLAG, FeedbackEntity.DEL_FLAG_NORMAL));
    /*
    dc.createAlias("feedback", "feedback"); // as

    dc.add(Restrictions.ne("id", 1L)); // !=

    if (StringUtils.isNotEmpty(feedback.getName())) {
        dc.add(Restrictions.like("name", "%" + feedback.getName() + "%")); //like
    }
    if (StringUtils.isNotEmpty(feedback.getName())) {
        dc.add(Restrictions.eq("name", feedback.getName())); //=
    }

	dc.addOrder(Order.asc("office.id")).addOrder(Order.asc("name"));  //排序

    */
        List<FeedbackEntity> entities = feedbackDao.find(dc);
        for (FeedbackEntity feedbackEntity : entities) {
            FeedbackVO feedbackVO = new FeedbackVO();
            beanMapper.map(feedbackEntity, feedbackVO);
            list.add(feedbackVO);
        }
        return list;
    }


    /**
     * 通过id查询数据
     *
     * @param id 数据标识
     * @return 数据对象
     */
    public FeedbackVO get(String id) {
        FeedbackVO feedback = new FeedbackVO();
        FeedbackEntity feedbackEntity = feedbackDao.findOne(Long.parseLong(id));
        if (feedbackEntity == null) {
            feedbackEntity = new FeedbackEntity();
        }
        beanMapper.map(feedbackEntity, feedback);
        return feedback;
    }


    /**
     * 根据条件分页查询
     *
     * @param page
     * @param feedback
     * @return
     */
    public Page<FeedbackVO> findFeedbackByPage(Page<FeedbackVO> page, FeedbackVO feedback) {
        Page<FeedbackEntity> pageEntity = new Page<FeedbackEntity>(page.getPageNo(), page.getPageSize());
        DetachedCriteria dc = feedbackDao.createDetachedCriteria();
        dc.add(Restrictions.eq(FeedbackEntity.DEL_FLAG, FeedbackEntity.DEL_FLAG_NORMAL));
        if (!StringUtils.isNotEmpty(page.getOrderBy())) {
            dc.addOrder(Order.desc("id"));
        }
        pageEntity = feedbackDao.find(pageEntity, dc);
        beanMapper.map(pageEntity, page);
        List<FeedbackVO> list = Lists.newArrayList();
        for (FeedbackEntity feedbackEntity : pageEntity.getList()) {
            FeedbackVO feedbackVO = new FeedbackVO();
            beanMapper.map(feedbackEntity, feedbackVO);
            list.add(feedbackVO);
        }
        page.setList(list);
        return page;
    }


}
